package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.enums.OrderEnum;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.form.FormProduct;
import com.huahang.fashionpress.service.impl.AddressServiceImpl;
import com.huahang.fashionpress.service.impl.OrderServiceImpl;
import com.huahang.fashionpress.service.impl.ProductServiceImpl;
import com.huahang.fashionpress.vo.AddressVO;
import com.huahang.fashionpress.vo.OrderDetailVO;
import com.huahang.fashionpress.vo.OrderMasterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 * @author : 王翰聪
 * @since : 2019/08/28
 * @version : 1.0
 */

@Controller
@RequestMapping("/myorder")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private AddressServiceImpl addressService;

    @Autowired
    WebSocket webSocket;

    //test
    @Autowired
    private ProductServiceImpl productService;

    @RequestMapping("/")
    public ModelAndView myOrderView(HttpSession session, Map map){
        //获取用户id
        Integer userID = (Integer) session.getAttribute("userID");

        List<OrderMasterVO> orderList = orderService.selectOrderListByUserID(userID);
        if(orderList != null){
            map.put("orderList", orderList);
        }

        return new ModelAndView("client/order", map);
    }

    @RequestMapping("/orderselect")
    public ModelAndView selectOrderView(HttpSession session, Map map, Integer status){
        //获取用户id
        Integer userID = (Integer) session.getAttribute("userID");

        switch (status){
            case 1:{
                List<OrderMasterVO> orderList = orderService.selectOrderListByUserIDAndStatus(userID, OrderEnum.PREPARE_TO_PAY);
                if(orderList != null)
                    map.put("orderList", orderList);
                break;
            }
            case 2:{
                List<OrderMasterVO> orderList = orderService.selectOrderListByUserIDAndStatus(userID, OrderEnum.PREPARE_TO_POST);
                if(orderList != null)
                    map.put("orderList", orderList);
                break;
            }
            case 3:{
                List<OrderMasterVO> orderList = orderService.selectOrderListByUserIDAndStatus(userID, OrderEnum.PREPARE_TO_GET);
                if(orderList != null)
                    map.put("orderList", orderList);
                break;
            }
            case 4:{
                List<OrderMasterVO> orderList = orderService.selectOrderListByUserIDAndStatus(userID, OrderEnum.ORDER_FINISH);
                if(orderList != null)
                    map.put("orderList", orderList);
                break;
            }
            case 5:{
                List<OrderMasterVO> orderList = orderService.selectOrderListByUserIDAndStatus(userID, OrderEnum.PREPARE_TO_REFUNDMENT);
                if(orderList != null)
                    map.put("orderList", orderList);
                break;
            }
        }

        return new ModelAndView("client/order", map);

    }

    //单个订单展示页面
    @RequestMapping("/orderSingle")
    public ModelAndView orderSingle(String orderID, Map map){
        OrderDetailVO orderDetailVO = orderService.selectOrderDetails(orderID).get(0);

        map.put("order", orderDetailVO);

        return new ModelAndView("client/orderSingle", map);
    }

    //订单生成页面
    @RequestMapping("/orderBuild")
    public ModelAndView orderBuild(Map map, HttpSession session, List<FormProduct> formProductList) throws ResultException {
        Integer userID = (Integer) session.getAttribute("userID");
        System.out.println("aaa");

        //总价
        BigDecimal total = new BigDecimal(0);
        for(FormProduct formProduct : formProductList){
            total.add(formProduct.getProductPrice().multiply(new BigDecimal(formProduct.getProductQuantity())));
        }

        List<AddressVO> addressVOList = addressService.getAllAddress(userID);
        if(addressVOList == null || addressVOList.isEmpty())
            return new ModelAndView("redirect:/addAdress");
        System.out.println(addressVOList.isEmpty());
        map.put("address", addressVOList);
        map.put("productList", formProductList);
        map.put("total", total);

        return new ModelAndView("client/orderbuild", map);
    }
}
